package com.xcity.db.entity;

import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.db.mapper.ActivityMapper;
import com.xcity.game.activity2.ActivityImpl;
import com.xcity.util.DBUtils;

import naga.x.common.Updatable;
import naga.x.service.Service;
import naga.x.service.ServiceException;
import naga.x.util.ServiceUtils;

/**
 * 运营活动实体类
 * @author yang.li
 *
 */
public class Activity implements Service, Updatable {

	/** 活动唯一id */
	private int uniqueId;
	/** 活动名 */
	private String name;
	/** 活动描述 */
	private String desc;
	/** 活动开始时间 */
	private Date beginTime;
	/** 活动结束时间 */
	private Date endTime;
	/** 活动实现类 */
	private String implClass;
	/** 活动配置数据（JSON格式） */
	private String data;
	
	// 以下字段不存数据库
	/** 活动是否进行中 */
	private boolean active;
	/** 活动实现类对象 */
	private ActivityImpl impl;
	
	public static final byte STATE_OPENED = 1, STATE_CLOSED = 0;

	public int getUniqueId() {
		return uniqueId;
	}

	public void setUniqueId(int uniqueId) {
		this.uniqueId = uniqueId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public Date getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(Date beginTime) {
		this.beginTime = beginTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public String getImplClass() {
		return implClass;
	}

	public void setImplClass(String implClass) {
		this.implClass = implClass;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}
	
	public <T> T getDataObj(Class<T> type) {
		return JSON.parseObject(data, type);
	}
	
	public <T> T getDataObj(TypeReference<T> type) {
		return JSON.parseObject(data, type);
	}
	
	@JSONField(serialize = false)
	public ActivityImpl getImpl() {
		if (impl == null) {
			try {
				Class<? extends ActivityImpl> javaclass = (Class<? extends ActivityImpl>) Class.forName(implClass);
				impl = ServiceUtils.newService(javaclass, new Class<?>[] { Activity.class }, this);
			} catch (Exception e) {
				throw new IllegalArgumentException("activity's impl error:" + uniqueId + "->" + implClass, e);
			}
		}
		return impl;
	}
	
	public void init() {
		getImpl().init();
	}
	
	public void save() {
		getImpl().save();
		DBUtils.update(this, ActivityMapper.class);
	}

	@Override
	public void startup() throws ServiceException {
		active = true;
		getImpl().startup();
	}

	@Override
	public void shutdown() throws ServiceException {
		active = false;
		getImpl().shutdown();
	}

	@JSONField(serialize = false)
	@Override
	public String getId() {
		return getImpl().getId();
	}

	@Override
	public boolean update() {
		return getImpl().update();
	}
	
	public void setDirty() {
		getImpl().setDirty();
	}

	@Override
	public String toString() {
		return "Activity [uniqueId=" + uniqueId + ", name=" + name + ", desc=" + desc + ", beginTime=" + beginTime
				+ ", endTime=" + endTime + ", implClass=" + implClass + ", data=" + data + ", active=" + active + "]";
	}
	
}
